package com.qf.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class HttpURLConnHelper
{

	public static String loadDataFromServer(String url)
	{
		// Log.i("info", "详细数据为" + url);
		try
		{
			HttpURLConnection conn = (HttpURLConnection) new URL(url)
					.openConnection();
			Log.i("info", "详细数据为" + conn.getResponseCode() + ":" + url);
			if (conn.getResponseCode() == 200) // 打开连接成功
			{
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				InputStream is = conn.getInputStream();
				byte[] buffer = new byte[1024 * 8];
				int len = -1;
				while ((len = is.read(buffer)) != -1)
				{
					baos.write(buffer, 0, len);
				}
				Log.i("info", "下载之后的数据为" + baos.toString());
				return baos.toString();
			}

		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String loadDataFromServerutils(String url)
	{
		HttpClient client = new DefaultHttpClient();
		HttpResponse resp;
		try
		{
			resp = client.execute(new HttpGet(url));
			Log.i("info", "款到即发快睡觉的反馈了就");
			if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
			{
				Log.i("info", "详细数据为" + ":" + url);
				return EntityUtils.toString(resp.getEntity(), "utf-8");
			}

		}

		catch (IOException e)
		{
			e.printStackTrace();
		}

		return null;
	}
}
